package com.ruoyi.business.service.impl;

import com.alibaba.fastjson.JSON;
import com.ruoyi.business.constant.CarConstant;
import com.ruoyi.business.domain.BusCar;
import com.ruoyi.business.domain.BusRent;
import com.ruoyi.business.dto.CheckDto;
import com.ruoyi.business.mapper.BusCarMapper;
import com.ruoyi.business.mapper.BusRentMapper;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.ruoyi.business.domain.BusCheck;
import com.ruoyi.business.mapper.BusCheckMapper;
import com.ruoyi.business.service.BusCheckService;

import java.util.Date;
import java.util.List;

/**
*内容: 
*作者: cy
*/   
    
   
@Service
@Slf4j
public class BusCheckServiceImpl implements BusCheckService{

    @Resource
    private BusCheckMapper busCheckMapper;

    @Autowired
    private BusCarMapper busCarMapper;
    @Autowired
    private BusRentMapper busRentMapper;


    @Override
    public int deleteByPrimaryKey(Integer id) {
        return busCheckMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(BusCheck record) {
        return busCheckMapper.insert(record);
    }

    @Override
    public int insertSelective(BusCheck record) {
        return busCheckMapper.insertSelective(record);
    }

    @Override
    public BusCheck selectByPrimaryKey(Integer id) {
        return busCheckMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(BusCheck record) {
        return busCheckMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(BusCheck record) {
        return busCheckMapper.updateByPrimaryKey(record);
    }


    /**
     * 汽车入库
     * 1.插入check表
     * 2.修改出租单状态  变成已归还
     * 3.修改车辆状态  变成未出租
     *
     * @param busCheck
     * @return
     */
    @Override
    public Boolean doBackCar(BusCheck busCheck, LoginUser loginUser) {
        log.info("汽车入库{},当前用户id为{}", JSON.toJSONString(busCheck), loginUser.getUserId());
        String username  = loginUser.getUsername();
        busCheck.setCreateTime(new Date());
        busCheck.setCreateBy(username);
        busCheck.setUpdateTime(new Date());
        busCheck.setUpdateBy(username);
        busCheck.setDelFlag(CarConstant.DEL_FLAG_UNDEL);
        //查询出租单信息
        BusRent rent = busRentMapper.selectRentByRentSn(busCheck.getRentSn());
        //查询车辆信息
        BusCar busCar = busCarMapper.selectCarByCarNum(rent.getCarNumber());
        busCheck.setDeptId(busCar.getDeptId());
        //新增数据
        int i = busCheckMapper.insertSelective(busCheck);
        if (i >0){
            //修改出租单状态和车状态
            rent.setRentStatus(CarConstant.CAR_IS_BACK);
            rent.setUpdateTime(new Date());
            rent.setUpdateBy(username);
            int a = busRentMapper.updateByPrimaryKey(rent);
            if (a > 0){
                //车辆未出租
                busCar.setStatus(CarConstant.CAR_NO_RENT);
                busCar.setUpdateTime(new Date());
                busCar.setUpdateBy(username);
                int k = busCarMapper.updateBusCar(busCar);
                if (k > 0){
                    return true;
                }
            }
        }
        throw new ServiceException("汽车入库失败,请联系管理员");

    }

    /**
     * 分页查询检查单信息
     * @param checkDto
     * @param loginUser
     * @return
     */
    @Override
    public List<BusCheck> selectCheckPage(CheckDto checkDto, LoginUser loginUser) {

        return busCheckMapper.selectCheckPage(checkDto,loginUser);
    }

}
